import ArrayUtil from '@/ArrayUtil';
import BaseUtil from '@/BaseUtil';
import MathUtil from '@/MathUtil';
import SelectUtil from '@/SelectUtil';
import { OrderId } from '@/globals/order';
import VestCommon from 'code/Core/Common/VestCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 拉扯
 **/
export default class BossSpell_Hook extends AbilityBase {
    static instance: BossSpell_Hook;
    constructor(id: string = 'Mkl3') {
        super(id);
        BossSpell_Hook.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {}
    hook(source: unit) {
        let es = SelectUtil.getEnemyUnitsInRange(source, 800);
        let tar = ArrayUtil.randomElement(es);
        if (tar == null) return;
        let x = GetUnitX(source);
        let y = GetUnitY(source);
        let tx = GetUnitX(tar);
        let ty = GetUnitY(tar);
        let f = MathUtil.angleBetweenCoords(x, y, tx, ty);
        let hooks: unit[] = [];
        let isHooked = false;
        let isEnd = false;
        let mark = AddSpecialEffect(BossAbilityManagerInstance.SpellMark, tx, ty);
        BaseUtil.onTimer(0.05, (c) => {
            if (c <= 20 && !isHooked) {
                x = x + CosBJ(f) * 40;
                y = y + SinBJ(f) * 40;
                let h = VestCommon.getNoneModelVest(x, y, f);
                DzSetUnitModel(h, 'Abilities\\Weapons\\WardenMissile\\WardenMissile.mdl');
                SetUnitOwner(h, GetOwningPlayer(source), false);
                hooks.push(h);
                es = SelectUtil.getEnemyUnitsInRange(h, 75);
                // 命中
                if (es.length > 0) {
                    tar = es[0];
                    isHooked = true;
                    DamageSystemInstance.applyPhysicalDamage(tar, source, 150 * c);
                    IssueTargetOrderById(source, OrderId.Attack, tar);
                    StunSystemIntance.stun(tar, 2, source);
                }
            } else {
                if (!isHooked) {
                    let h = hooks.pop();
                    VestCommon.returnVest(h);
                }
            }
            if (isHooked) {
                let h = hooks.shift();
                tx = GetUnitX(h);
                ty = GetUnitY(h);
                VestCommon.returnVest(h);
                SetUnitX(source, tx);
                SetUnitY(source, ty);
                DestroyEffect(AddSpecialEffect('', x, y));
                SetUnitX(tar, x);
                SetUnitY(tar, y);
                if (hooks.length == 0) {
                    isEnd = true;
                    DestroyEffect(mark);
                    source = null;
                    mark = null;
                }
            }
            if (c >= 40) {
                isEnd = true;
                DestroyEffect(mark);
                source = null;
                mark = null;
            }
            return !isEnd;
        });
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        BossAbilityManagerInstance.text('拉扯', source);
        BaseUtil.runLater(1, () => {
            this.hook(source);
            source = null;
        });
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
}
